/*
 * @Author: lei
 * @Date: 2021-08-04 07:28:31
 * @LastEditTime: 2021-08-04 08:16:57
 * @LastEditors: Please set LastEditors
 * @Description: 使用移动构造函数和移动赋值运算符
 * @FilePath: /code/move_contructors.cpp
 */
#include <iostream>
using namespace std;

class A
{
public: 
    A(){cout<<"正在默认构造"<<endl;}
    A(int b):a(b){cout<<"正在单参构造！"<<endl;}
    void show(){
        cout<<"a= "<<this->a<<endl;
    }
    ~A(){cout<<"正在析构!"<<endl;}
    /*A(const A && b)
    {
        a=b.a;
        cout<<"正在移动！"<<endl;
    }*/
    explicit A(const A & b)//explicit 防止const A&& b隐式转换为const A& b
    {
        a=b.a;
        cout<<"正在拷贝构造！"<<endl;
    }
    A& operator=(const A & b)
    {
        a=b.a;
        cout<<"正在拷贝赋值！"<<endl;
        return *this;
    }
    A& operator=(const A && b)
    {
        a=b.a;
        cout<<"正在移动赋值！"<<endl;
        return *this;
    }

private:
    int a;
};

int main()
{
    A a(10);
    //A b=std::move(a);//如果自定义了拷贝构造函数，而没有自定义移动构造函数，这里会编译错误；
    A b;
    b=std::move(a);
    return 0;
}